﻿package
{
	import com.google.maps.InfoWindowOptions;
	import com.google.maps.LatLng;
	import com.google.maps.LatLngBounds;
	import com.google.maps.Map;
	import com.google.maps.MapEvent;
	import com.google.maps.MapMouseEvent;
	import com.google.maps.MapMoveEvent;
	import com.google.maps.MapOptions;
	import com.google.maps.MapType;
	import com.google.maps.controls.MapTypeControl;
	import com.google.maps.controls.ZoomControl;
	import com.google.maps.interfaces.IPolyline;
	import com.google.maps.overlays.Marker;
	import com.google.maps.overlays.MarkerOptions;
	import com.google.maps.services.*;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.*;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.sensors.Geolocation;
	
	import gmapAssets.InfoMarker;
	import gmapAssets.InfoWindow;
		
	public class GMap extends MovieClip
	{
		private var myTextLoader:URLLoader = new URLLoader();
		private var mapKey:String;
		public var map:Map;
		private var me_mc:MeClip;
		private var meMarker:Marker;
		private var previousSelectedMarker:Marker;
		private var currentSelectedMarker:Marker;
		private var m:Marker;
		private var marker_mc:InfoMarker;
		public var infoWindow:InfoWindow;
		private var showMeStatus:Boolean = false;
		
		public function GMap()
		{
			myTextLoader.load(new URLRequest("appid.txt"));
			myTextLoader.addEventListener(Event.COMPLETE, onLoaded);
		}
		private function onLoaded(e:Event):void {
			mapKey = String(e.target.data);
			setupMap();
		}
		private function setupMap():void
		{
			map = new Map();
			map.url = "http://foxdemon.com";
			map.key = mapKey;
			map.setSize(new Point(704,530));
			map.addEventListener(MapEvent.MAP_READY, onMapReady);
			map.addEventListener(TransformGestureEvent.GESTURE_ZOOM, pinchZoom); 
			map.sensor = "true";
			this.addChild(map);
		}
		private function onMapPreinitialize(event:MapEvent):void {
			map.setInitOptions(
				new MapOptions({
					center: new LatLng(4.109495,109.101269),
					zoom: 5,
					mapType: MapType.SATELLITE_MAP_TYPE
				}));
		}
		private function pinchZoom(e:TransformGestureEvent):void 
		{ 
			if( e.scaleX < 1 ) 
				map.setZoom(map.getZoom() -0.1 , true); 
			else 
				map.setZoom(map.getZoom() +0.1 , true); 
		}
		private function onMapReady(event:Event):void
		{
			//map.addControl(new ZoomControl());
			//map.addControl(new MapTypeControl());
			
			this.dispatchEvent(new Event("MAP_READY",true));
		}
		public function setMapCenter(latLng:LatLng,_zoom:int){
			map.setCenter(latLng, _zoom);	
		}
					
		public function addMarker(_data:Object):void{
			var lat:Number = _data.pos.x;
			var lng:Number = _data.pos.y;
			infoWindow = new InfoWindow();
			infoWindow.data = _data;
			infoWindow.title = _data.name;
			infoWindow.description = _data.pos.addr;
			infoWindow.x = 0;
			
			var windowOption:MarkerOptions;
			windowOption = new MarkerOptions();
			windowOption.icon = infoWindow;
			windowOption.iconOffset = new Point(infoWindow.getOffsetX(), infoWindow.getOffsetY());
			
			var infoWindowMarker:Marker = new Marker(new LatLng(lat,lng), windowOption);
			infoWindowMarker.visible = false;
			map.addOverlay(infoWindowMarker);
			marker_mc = new InfoMarker();			
			marker_mc.infoWindowMarker = infoWindowMarker;
			
			if(!marker_mc.checked)
			{
				marker_mc.checked = true;
				m = new Marker(new LatLng(lat,lng),new MarkerOptions({icon:marker_mc}));
				map.addOverlay(m);
			}
			
			m.addEventListener(MapMouseEvent.CLICK, markerHandler);
			infoWindow.bg.addEventListener("WINDOW_SELECTED", bgHandler);
		}
	
		private function bgHandler(evt:Event):void
		{
			currentSelectedMarker.visible = false;
		}
		
		protected function markerHandler(event:MapMouseEvent):void
		{
			if(currentSelectedMarker!=null)
			{				
				previousSelectedMarker = currentSelectedMarker;
				previousSelectedMarker.visible = false;			
			}		
			
			currentSelectedMarker = event.target.getOptions().icon.infoWindowMarker;
			currentSelectedMarker.visible = true;
			currentSelectedMarker.pane.bringToTop(currentSelectedMarker);
		}
		public function showMe(lat,lng):void{
			
			trace("show lat : "+lat);			
			map.setCenter(new LatLng(lat,lng), 14);
			if(!showMeStatus){
				me_mc = new MeClip();
				meMarker = new Marker(new LatLng(lat,lng),new MarkerOptions({icon:me_mc}));
				map.addOverlay(meMarker);
				showMeStatus = true;
			}else{
				meMarker.setLatLng(new LatLng(lat,lng));
			}
		}
		public function clearMap():void
		{
			map.clearOverlays();
		}
	}
}